using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Nakio.SystemAdministration.CustomClasses
{
    internal class Win32Interop
    {
        public const uint SHGFI_DISPLAYNAME = 0x00000200;
        public const uint SHGFI_TYPENAME = 0x400;
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0;    // 'Large icon
        public const uint SHGFI_SMALLICON = 0x1;    // 'Small icon

        [DllImport("shell32.dll")]
        private static extern IntPtr SHGetFileInfo(string pszPath,
                                    uint dwFileAttributes,
                                    ref SHFILEINFO psfi,
                                    uint cbSizeFileInfo,
                                    uint uFlags);

        public static Bitmap GetSmallIconFile(string filePath)
        {
            var shinfo = new SHFILEINFO();
            IntPtr hImgSmall = SHGetFileInfo(filePath, 0, ref shinfo,
                                             (uint)Marshal.SizeOf(shinfo),
                                             SHGFI_ICON |
                                             SHGFI_SMALLICON);

            return Icon.FromHandle(shinfo.hIcon).ToBitmap();            
        }

        public static Bitmap GetLargeIconFile(string filePath)
        {
            var shinfo = new SHFILEINFO();
            IntPtr hImgLarge = SHGetFileInfo(filePath, 0,
                                             ref shinfo, (uint)Marshal.SizeOf(shinfo),
                                             SHGFI_ICON | SHGFI_LARGEICON);

            return Icon.FromHandle(shinfo.hIcon).ToBitmap();
        }

        public static string GetFileType(string filePath)
        {
            var shinfo = new SHFILEINFO();
            IntPtr i = SHGetFileInfo(filePath, 0, ref shinfo,
                                     (uint)Marshal.SizeOf(shinfo),SHGFI_TYPENAME);

            return Convert.ToString(shinfo.szTypeName.Trim());
        }
    }
}